package com.tianji.learning.service.impl;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.api.cache.CategoryCache;
import com.tianji.api.client.course.CatalogueClient;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.client.search.SearchClient;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.course.CataSimpleInfoDTO;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.api.dto.user.UserDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.QuestionFormDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.domain.query.QuestionAdminPageQuery;
import com.tianji.learning.domain.query.QuestionPageQuery;
import com.tianji.learning.domain.vo.QuestionAdminVO;
import com.tianji.learning.domain.vo.QuestionVO;
import com.tianji.learning.mapper.InteractionQuestionMapper;
import com.tianji.learning.service.IInteractionQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 互动提问的问题表 服务实现类
 * </p>
 *
 * @author 晚风
 * @since 2023-09-23
 */
@Service
@RequiredArgsConstructor
public class InteractionQuestionServiceImpl extends ServiceImpl<InteractionQuestionMapper, InteractionQuestion> implements IInteractionQuestionService {

    private final IInteractionReplyService replyService;
    private final UserClient userClient;
    private final SearchClient searchClient;
    private final CourseClient courseClient;
    private final CatalogueClient catalogueClient;
    private final CategoryCache categoryCache;
    @Override
    public void saveQuestion(QuestionFormDTO dto) {
        // 1.获取当前用户id
        Long userId = UserContext.getUser();
        // 2.dto转换为po
        InteractionQuestion question = BeanUtils.copyBean(dto, InteractionQuestion.class);
        question.setUserId(userId);
        // 3. 保存
        this.save(question);
    }

    @Override
    public void updateQuestion(Long id, QuestionFormDTO dto) {
        // 1. 校验
        if (StringUtils.isBlank(dto.getTitle()) || StringUtils.isBlank(dto.getDescription()) || dto.getAnonymity() == null){
            throw new BadRequestException("非法参数");
        }
        // 校验id
        InteractionQuestion question = this.getById(id);
        if (question == null){
            throw new BadRequestException("非法参数");
        }
        // 校验用户，不能修改别人的互动问题
        Long userId = UserContext.getUser();
        if (!userId.equals(question.getUserId())){
            throw new BadRequestException("不能修改别人的互动问题");
        }
        // 2.dto转po
        question.setTitle(dto.getTitle());
        question.setAnonymity(dto.getAnonymity());
        question.setDescription(dto.getDescription());
        // 3.修改
        this.updateById(question);
    }

    @Override
    public PageDTO<QuestionVO> queryQuestionPage(QuestionPageQuery query) {
        // 1. 校验参数 courseId
        if (query.getCourseId() == null){
            throw new BadRequestException("课程id不能为空");
        }
        // 2. 获取登录用户id
        Long userId = UserContext.getUser();
        // 3. 分页查询互动问题 interaction_question 条件 courseId  onlyMine 为true 才会加userId 小节id不为空  hidde为false 分页查询  按提问时间倒序
        Page<InteractionQuestion> page = this.lambdaQuery()
                //tableFieldInfo.getProperty()获取InteractionQuestion实体类的属性名称
                .select(InteractionQuestion.class, tableFieldInfo -> !tableFieldInfo.getProperty().equals("description"))// 指定不查的字段
                .eq(InteractionQuestion::getCourseId, query.getCourseId())
                .eq(query.getOnlyMine(), InteractionQuestion::getUserId, userId)
                .eq(query.getSectionId() != null, InteractionQuestion::getSectionId, query.getSectionId())
                .eq(InteractionQuestion::getHidden, false)
                .page(query.toMpPageDefaultSortByCreateTimeDesc());
        List<InteractionQuestion> records = page.getRecords();
        if (CollUtils.isEmpty(records)){
            return PageDTO.empty(page);
        }

        Set<Long> latestAnswerIds = new HashSet<>(); // 互动问题的，最新回答id集合
        Set<Long> userIds = new HashSet<>(); // 互动问题的用户id集合
        for (InteractionQuestion record : records) {
            if (!record.getAnonymity()){ //如果用户是匿名提问  则不显示名称和头像
                userIds.add(record.getUserId());
            }
            if (record.getLatestAnswerId() != null){
                latestAnswerIds.add(record.getLatestAnswerId());
            }
        }
/*        Set<Long> latestAnswerId = records.stream()
                .filter(c->c.getLatestAnswerId() != null)
                .map(InteractionQuestion::getLatestAnswerId).collect(Collectors.toSet());*/
        // 4. 根据最新回答id  批量查询回答信息
        Map<Long,InteractionReply> replyMap = new HashMap<>();
        if (CollUtils.isNotEmpty(latestAnswerIds)){
//            List<InteractionReply> replyList = replyService.listByIds(latestAnswerIds);
            List<InteractionReply> replyList = replyService.list(Wrappers.<InteractionReply>lambdaQuery()
                    .in(InteractionReply::getId, latestAnswerIds).eq(InteractionReply::getHidden, false));
            for (InteractionReply reply : replyList) {
                if (!reply.getAnonymity()){
                    userIds.add(reply.getUserId()); // 将最新回答的用户id 存入userIds
                }
                replyMap.put(reply.getId(),reply);
            }
//            Map<Long, InteractionReply> collect = replyList.stream().collect(Collectors.toMap(InteractionReply::getId, c -> c));
        }

        // 5. 远程调用用户服务 获取用户信息 批量
        List<UserDTO> userDTOS = userClient.queryUserByIds(userIds);
        Map<Long, UserDTO> userDTOMap = userDTOS.stream().collect(Collectors.toMap(UserDTO::getId, c -> c));

        // 6. 封装vo返回
        List<QuestionVO> voList = new ArrayList<>();
        for (InteractionQuestion record : records) {
            QuestionVO vo = BeanUtils.copyBean(record, QuestionVO.class);
            if (!vo.getAnonymity()){
                UserDTO userDTO = userDTOMap.get(record.getUserId());
                if (userDTO != null){
                    vo.setUserName(userDTO.getName());
                    vo.setUserIcon(userDTO.getIcon());
                }
            }
            InteractionReply reply = replyMap.get(record.getLatestAnswerId());
            if (reply != null){
                if (!reply.getAnonymity()){ // 最新回答 如果是非匿名  才设置 最新回答者的昵称
                    UserDTO userDTO = userDTOMap.get(record.getUserId());
                    if (userDTO != null){
                        vo.setLatestReplyUser(userDTO.getName()); // 最新回答者的昵称
                    }
                }
                vo.setLatestReplyContent(reply.getContent()); // 最新回答信息
            }
            voList.add(vo);
        }
        return PageDTO.of(page,voList);
  }

    @Override
    public QuestionVO queryQuestionById(Long id) {
        // 1. 校验
        if (id == null){
            throw new BadRequestException("非法参数");
        }
        // 2. 查询互动问题表  按主键查询
        InteractionQuestion question = this.getById(id);
        if (question == null) {
            throw new BadRequestException("问题不存在");
        }
        // 3. 如果该问题管理员设置了隐藏  返回空
        if (question.getHidden()){
            return null;
        }
        // 4. 封装vo返回
        QuestionVO questionVO = BeanUtils.copyBean(question, QuestionVO.class);
        // 5. 如果用户是匿名提问， 不用查询提问者的昵称和头像
        if (!question.getAnonymity()){
            UserDTO userDTO = userClient.queryUserById(question.getUserId());
            if (userDTO !=null){
                questionVO.setUserName(userDTO.getName());
                questionVO.setUserIcon(userDTO.getIcon());
            }
        }
        return questionVO;
    }

    @Override
    public PageDTO<QuestionAdminVO> queryQuestionAdminVOPage(QuestionAdminPageQuery query) {
        // 0. 如果用户传了课程名称参数，则从es中获取该名称对应的课程id
        String courseName = query.getCourseName();
        List<Long> ids = null;
        if (StringUtils.isNotBlank(courseName)){
            ids = searchClient.queryCoursesIdByName(courseName); // 通过feign远程调用搜索服务，从es中搜索该关键字对应的课程id
            if (CollUtils.isEmpty(ids)){
                return PageDTO.empty(0L,0L);
            }
        }
        // 1. 查询互动问题表  条件前端条件了就添加条件   分页  排序按提问时间倒序
        Page<InteractionQuestion> page = this.lambdaQuery()
                .in(CollUtils.isNotEmpty(ids),InteractionQuestion::getCourseId, ids)
                .like(StringUtils.isNotBlank(query.getCourseName()), InteractionQuestion::getTitle, query.getCourseName())
                .eq(query.getStatus() != null, InteractionQuestion::getStatus, query.getStatus())
                .between(query.getBeginTime() != null && query.getEndTime() != null,
                        InteractionQuestion::getCreateTime, query.getBeginTime(), query.getEndTime())
                .page(query.toMpPageDefaultSortByCreateTimeDesc());
        List<InteractionQuestion> records = page.getRecords();
        if (CollUtils.isEmpty(records)){
            return PageDTO.empty(0L,0L);
        }

        Set<Long> uids = new HashSet<>(); // 用户id集合
        Set<Long> courseids = new HashSet<>(); // 课程id集合
        Set<Long> chapterAndSectionIds = new HashSet<>(); // 章和节的id集合
        for (InteractionQuestion record : records) {
            uids.add(record.getUserId());
            courseids.add(record.getCourseId());
            chapterAndSectionIds.add(record.getChapterId()); // 章id
            chapterAndSectionIds.add(record.getSectionId()); // 节id
        }
        // 2. 远程调用用户服务  获取用户信息
        List<UserDTO> userDTOS = userClient.queryUserByIds(uids);
        if (CollUtils.isEmpty(userDTOS)){
            throw new BizIllegalException("用户不存在");
        }
        Map<Long, UserDTO> userDTOMap = userDTOS.stream().collect(Collectors.toMap(UserDTO::getId, c -> c));

        // 3. 远程调用用户服务  获取课程信息
        List<CourseSimpleInfoDTO> cinfos = courseClient.getSimpleInfoList(courseids);
        if (CollUtils.isEmpty(cinfos)){
            throw new BizIllegalException("课程不存在");
        }
        Map<Long, CourseSimpleInfoDTO> infoDTOMap = cinfos.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));
        // 4. 远程调用课程服务  获取章节信息
        List<CataSimpleInfoDTO> cataSimpleInfoDTOS = catalogueClient.batchQueryCatalogue(chapterAndSectionIds);
        if (CollUtils.isEmpty(cataSimpleInfoDTOS)){
            throw new BizIllegalException("章节不存在");
        }
        Map<Long, String> longStringMap = cataSimpleInfoDTOS.stream().collect(Collectors.toMap(CataSimpleInfoDTO::getId, c -> c.getName()));

        // 5. 获取分类信息

        // 6. 封装vo返回
        List<QuestionAdminVO> voList = new ArrayList<>();
        for (InteractionQuestion record : records) {
            QuestionAdminVO adminVo = new QuestionAdminVO();
            UserDTO userDTO = userDTOMap.get(record.getUserId());
            if (userDTO != null){
                adminVo.setUserName(userDTO.getName()); // 提问者昵称
            }
            CourseSimpleInfoDTO infoDTO = infoDTOMap.get(record.getCourseId());
            if (infoDTO != null){
                adminVo.setCourseName(infoDTO.getName()); // 课程名称
                List<Long> categoryIds = infoDTO.getCategoryIds();
                String categoryNames = categoryCache.getCategoryNames(categoryIds);
                adminVo.setCategoryName(categoryNames); // 分类名称，拼接字段
            }
            adminVo.setCategoryName(longStringMap.get(record.getChapterId()) == null ? "" : longStringMap.get(record.getChapterId()));// 章名称
            adminVo.setSectionName(longStringMap.get(record.getSectionId())); // 节名称

            voList.add(adminVo);
        }
        return PageDTO.of(page,voList);
    }
}
